package providertype

import (
	"encoding/json"
	"strconv"

	// "time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"

	// "github.com/jinzhu/gorm"

	"oxy_server/model"
	"oxy_server/module/log"
	"oxy_server/until"
)

type (
	ProviderType struct {
		ID   bson.ObjectId `json:"_id" bson:"_id"`
		Name string        `json:"name" bson:"name"`
		Code string        `json:"code" bson:"code"`
	}
	InsertProviderType struct {
		Name string `json:"name" bson:"name"`
		Code string `json:"code" bson:"code"`
	}
)
type OXYProviderType struct{}

func NewOXYProviderType() *OXYProviderType {
	Data := &OXYProviderType{}
	return Data
}

func (ro *OXYProviderType) GetListHandler(c echo.Context) error {
	currentpage, err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	sort := c.QueryParam("sort")
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "Provider_type"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	query, count, err := model.DB().QuerySort(M, currentpage, perpage, sort)
	if err != nil {
		log.Error("查询出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	ps := []ProviderType{}
	query.All(&ps)

	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":  string(str),
		"count": count,
	})

	return nil

}

func (ro *OXYProviderType) GetInfoHandler(c echo.Context) error {
	log.Info("GetRoleInfoHandler")
	id := c.QueryParam("id")
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "Provider_type"
	query, err := model.DB().QueryOne(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return nil
	}
	p := ProviderType{}
	query.One(&p)
	str, err := json.Marshal(p)
	if err != nil {
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return nil
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
		"data": string(str),
	})

	return nil
}

func (ro *OXYProviderType) AddHandler(c echo.Context) error {
	s := &InsertProviderType{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	model.DB().Document = "Provider_type"
	err := model.DB().Insert(s)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}

func (ro *OXYProviderType) UpdateHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("UpdateSupplierTableHandler", id)

	s := &InsertProviderType{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	model.DB().Document = "Provider_type"
	err := model.DB().Update(bson.M{"_id": bson.ObjectIdHex(id)}, s)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
func (ro *OXYProviderType) DelHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelSupplierTableHandler", id)
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "Provider_type"
	err := model.DB().Remove(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		log.Error("删除出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
